package com.atguigu.service.impl;

import com.atguigu.dao.UserDao;
import com.atguigu.dao.impl.UserDaoImpl;
import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.utils.MD5Utils;

/**
 * @Author luoyin
 * @Date 18:06 2022/7/15
 **/
public class UserServiceImpl implements UserService {
    @Override
    public User getNamePwdService(String username, String password) throws Exception {
        UserDao userDao = new UserDaoImpl();
        return userDao.getNamePwdDao(username,password);
    }

    @Override
    public User selectByNameService(String userName) throws Exception {
        UserDao userDao = new UserDaoImpl();
        return userDao.selectByNameDao(userName);
    }

    @Override
    public int registService(User user) throws Exception {
        UserDao userDao = new UserDaoImpl();
        //获取输入密码
        String userPwd = user.getUserPwd();
        //获取加密的盐
        String salt = MD5Utils.generateSalt();
        //使用盐和密码进行加密
        String md5UserPwd = MD5Utils.generateMD5AndSalt(userPwd, salt);
        //把user中的密码改成加密模式
        user.setUserPwd(md5UserPwd);
        return userDao.addDao(user);
    }

    @Override
    public User loginService(String username, String password) throws Exception {
        UserDao userDao = new UserDaoImpl();
        User user = userDao.selectByNameDao(username);
        //不存在该账户
        if(user == null){
            return null;
        }

        return MD5Utils.verify(password, user.getUserPwd()) ? user : null;
    }
}
